package com.wash.shoes.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import com.wash.shoes.common.enums.FactoryShelvesStatusEnum;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wash.shoes.entity.FactoryshelvesPo;
import com.wash.shoes.entity.FactoryshelvesRackGridPo;
import com.wash.shoes.mapper.FactoryShelvesMapper;
import com.wash.shoes.mapper.FactoryshelvesRackGridMapper;
import com.wash.shoes.service.FactoryShelvesService;

import cn.hutool.core.lang.Assert;
import lombok.RequiredArgsConstructor;

/**
 * 工厂货架服务实现类
 */
@RequiredArgsConstructor
@Service
public class FactoryShelvesServiceImpl extends ServiceImpl<FactoryShelvesMapper, FactoryshelvesPo>
    implements FactoryShelvesService {

    private final FactoryshelvesRackGridMapper factoryshelvesRackGridMapper;

    /**
     * 修改货架使用数量
     **/
    @Override
    public Integer updateUseNumInteger(String id) {
        return getBaseMapper().updateUseNumInteger(id);
    }

    @Override
    @Transactional
    public boolean saveOrUpdate(FactoryshelvesPo entity) {
        FactoryshelvesPo factoryshelvesPo = getBaseMapper()
            .selectOne(new LambdaQueryWrapper<FactoryshelvesPo>().eq(FactoryshelvesPo::getCode, entity.getCode()));
        //修改的场景,使用数量保持原来的,前端没有提交使用数量上来
        entity.setUseNum(Objects.nonNull(factoryshelvesPo)?factoryshelvesPo.getUseNum():0);
        // 新增的场景
        if (Objects.isNull(entity.getId())) {
            entity.setId(IdWorker.getIdStr());
            Assert.isNull(factoryshelvesPo, "此编号已存在");

            List<FactoryshelvesRackGridPo> data = getFactoryshelvesRackGridPos(1, entity);
            factoryshelvesRackGridMapper.insert(data);
        } else {
            //获取原来配置的格子容量看是否又变更格子容量
            FactoryshelvesPo factoryshelvesRackGridPo =   getBaseMapper().selectById(entity.getId());
            if (null==factoryshelvesRackGridPo) {
                //新增容量
                List<FactoryshelvesRackGridPo> data = getFactoryshelvesRackGridPos(1, entity);
                factoryshelvesRackGridMapper.insert(data);
            }else
            //查看是否改了容量
            if(!factoryshelvesRackGridPo.getCapacity().equals(entity.getCapacity())){
                //就表示是又减少容量
                if(factoryshelvesRackGridPo.getCapacity()>entity.getCapacity()){
                    //如果原来货架上又鞋子就不能让他减,可以增加
                    Long count =factoryshelvesRackGridMapper.selectCount(new LambdaQueryWrapper<FactoryshelvesRackGridPo>().eq(FactoryshelvesRackGridPo::getFactoryshelvesId, entity.getId()).isNotNull(FactoryshelvesRackGridPo::getOrderId));
                    Assert.isTrue(count == 0, "此货架已存在订单不能缩减格子");
                    // 删除此货架的格子
                    factoryshelvesRackGridMapper.delete(new LambdaQueryWrapper<FactoryshelvesRackGridPo>().eq(FactoryshelvesRackGridPo::getFactoryshelvesId, entity.getId()));

                    //新生成格子
                    List<FactoryshelvesRackGridPo> data = getFactoryshelvesRackGridPos(1, entity);
                    factoryshelvesRackGridMapper.insert(data);
                }else {
                    //新增容量
                    List<FactoryshelvesRackGridPo> data = getFactoryshelvesRackGridPos(factoryshelvesRackGridPo.getCapacity() + 1, entity);
                    factoryshelvesRackGridMapper.insert(data);
                }
                //没得格子的情况
            } else  {
                // 修改鞋架状态和格子状态一致
                FactoryshelvesRackGridPo entityTemp = new FactoryshelvesRackGridPo();
                entityTemp.setStatus(entity.getStatus());
                factoryshelvesRackGridMapper.update(entityTemp, new LambdaQueryWrapper<FactoryshelvesRackGridPo>()
                        .eq(FactoryshelvesRackGridPo::getFactoryshelvesId, entity.getId()));
            }
        }
        return super.saveOrUpdate(entity);
    }

    private static List<FactoryshelvesRackGridPo> getFactoryshelvesRackGridPos(int factoryshelvesRackGridPo, FactoryshelvesPo entity) {
        //新生成格子
        List<FactoryshelvesRackGridPo> data = new ArrayList<>();
        for (int i = factoryshelvesRackGridPo; i <= entity.getCapacity(); i++) {
            FactoryshelvesRackGridPo newfactoryshelvesRackGridPo = new FactoryshelvesRackGridPo();
            newfactoryshelvesRackGridPo.setFactoryId(entity.getFactoryId());
            newfactoryshelvesRackGridPo.setFactoryshelvesId(entity.getId());
            newfactoryshelvesRackGridPo.setShowCode(entity.getPrefix() + i);
            newfactoryshelvesRackGridPo.setSortNum(i);
            newfactoryshelvesRackGridPo.setStatus(FactoryShelvesStatusEnum.ONLINE);// 设置正常状态
            data.add(newfactoryshelvesRackGridPo);
        }
        return data;
    }
}
